#pragma once


#include "stdafx.h"
#include "resource.h"

//extern "C"{
#include <iostream>
//};
#include <string>
#include <fstream>
#include <stdio.h>
#include "atlcoll.h"

#include "TcpClient.h"
#include "RegistryAccess.h"
#include "StringFunctions.h"

using namespace std;



	int tmpNo = 0;

	string callserver(string message)
	{
		string messageFile = dropmessagefile(message);


		string result = (string)"";
		WSADATA wsaData = {0};
		WORD wVer = MAKEWORD(2,2);
		int nRet = WSAStartup( wVer, &wsaData );
		if( nRet == SOCKET_ERROR ) {
	//	cout << "Failed to init Winsock library" << endl;
		return result;
		}
//		cout << "Opening connection to server" << endl;
		WORD WSAEvent = 0;
		WORD WSAErr = 0;
		SOCKET hServer = {0};
		// open a socket
		//
		// for the server we do not want to specify a network address
		// we should always use INADDR_ANY to allow the protocal stack
		// to assign a local IP address
		hServer = socket( AF_INET, SOCK_STREAM, IPPROTO_IP );
		if( hServer == INVALID_SOCKET ) {
			return result;
		}
		// name a socket
		sockaddr_in saServer = {0};
		saServer.sin_family = PF_INET;
		saServer.sin_port = htons( 5005 );
		saServer.sin_addr.s_addr = inet_addr( "127.0.0.1" );
		// connect
		nRet = connect( hServer, (sockaddr*)&saServer, sizeof( sockaddr ) );
		if( nRet == SOCKET_ERROR ) {
			closesocket( hServer );
			return result;
		}

		// process data
		char wzRec[1024];
//		message.copy(wzRec,message.length()+1,0);
		int nLeft = 1024;
		int iPos = 0;
		int nData = 0;
//		if( argc == 2 ) {
		// copy input string from command argument
		strcpy_s( wzRec, 1024, messageFile.c_str() );
	//	}
		do
		{
			nData = send( hServer, &wzRec[iPos], nLeft, 0 );
			if( nData == SOCKET_ERROR ) {
		//	cout << "Error sending data" << endl;
				break;
			}
			nLeft -= nData;
			iPos += nData;
		} while( nLeft > 0 );
		// clear data buffer
		memset( &wzRec, 0, sizeof( wzRec ) );
		nLeft = 1024;
		iPos = 0;
		do
		{
			nData = recv( hServer, &wzRec[iPos], 1024, 0 );
			if( nData == SOCKET_ERROR ) {
		//	cout << "Error receiving data" << endl;
				break;
			}
			nLeft -= nData;
			iPos += nData;
		} while( nData > 0  && iPos < (0) );
	//	cout << "Data Echoed: " << wzRec << endl;
	//	cout << "Closing connection" << endl;
		 
		// shutdown socket
		nRet = shutdown( hServer, SD_BOTH );
		if( nRet == SOCKET_ERROR ) {
		// WSAGetLastError()
	//	cout << "Error trying to perform shutdown on socket" << endl;
			return result;
		}
		// close server socket
		nRet = closesocket( hServer );
		hServer = 0;
		if( nRet == SOCKET_ERROR ) {
	//	cout << "Error failed to close socket" << endl;
			return result;
		}
		// Release WinSock DLL
		nRet = WSACleanup();
		if( nRet == SOCKET_ERROR ) {
		//	cout << "Error cleaning up Winsock Library" << endl;
			return result;
		}
		string s (wzRec);

		string content = getfilecontents(s);

		return  content;
//		cout << "Data sent successfully" << endl;

	}

	string dropmessagefile(string message)
	{
		CComBSTR regKey="Software\\LizardTF";
		CComBSTR valueName = "TempFolder";

		CComBSTR tmpFolder = GetRegistryKeyValue(regKey, valueName).m_str;

		CComBSTR tmpFile = tmpFolder;
		tmpFile.Append("\\LizardMessage");
		tmpFile.Append(InttoString( tmpNo++).c_str());


		try
		{
			string f = CComBSTRtoString(tmpFile);
			remove(f.c_str());
		}
		catch (...)
		{}

		ofstream out(tmpFile);
		out.write(message.c_str(),message.length());
		out.flush();
		out.close();
		return CComBSTRtoString(tmpFile);
	}

	string getfilecontents(string filename)
	{
		string buf="";
		string line;
		ifstream in(filename.c_str());
		if (!in)
			return buf;

		while(std::getline(in,line))
			buf += line + "\n";

		remove(filename.c_str());

		return buf;
	}

void CallMenuFunction(string menuId, string paths)
	{
		string strReq = "CALLMENU|";
		strReq += menuId+"|";
		strReq += paths;

//		messageHandler msgH;

//		string strResp = msgH.callserver(strReq);
			string strResp = callserver(strReq);
	
		
	}

	string GetColData(string paths)
	{
		string strReq = "GETFILEINFOS|";
		strReq += paths;

	//	messageHandler msgH;

		string strResp = callserver(strReq);
		
		return strResp;
	}

	string GetLizardStatus(string paths)
	{
		string strReq = "GETLIZARDSTATUS|";
		strReq += paths;

	//	messageHandler msgH;

		string strResp = callserver(strReq);
		
		return strResp;
	}

	string GetIconStatus(string iconType, string path)
	{
		string strReq = "GETICONSTATUS|";
		strReq += path;
		strReq += "|";
		strReq += iconType;

//		messageHandler msgH;

		string strResp = callserver(strReq);
		
		return strResp;
	}